-- base.lua
-- 
-- this file contains all the basic function calls required for the project
-- 
-- also includes global data
-- 

if not base_lua_loaded == true then

  base_lua_loaded = true 
  Teams = {}
  Teams.hiders  = {}
  Teams.seekers = {}
  
  Teams.hiders.Name  = "Hiders"
  Teams.seekers.Name = "Seekers"
  
  Teams.Player = {}
  Teams.Player.Perk = {}
  
  Teams.Player.Inventory = {}
  Teams.Player.Inventory.Limit = 100 -- this number is to be changed
  
  Teams.Player.Inventory.Item = {}
  
  print( "Teams initialized" )
  print( "" )
  
end

-- Player contructor
-- 
-- Inputs:
--  name   - name of player
--  entity - entity interface for GMOD
--  Team   - the team the player belongs to
--
-- Returns:
--  The newly constructed Player
-- 
Teams.Player.new = function( name, entity, team )
  
  local self = {}
  self.Name   = name   or nil
  self.Entity = entity or nil
  self.Team   = team   or nil
  self.Money  = 0 
  self.Inventory = Teams.Player.Inventory.new( self ) 
  
  self.PerkOMeter = 0 
  self.Perks = {}
  
  self.AddPerk = function( perk )-- the perk needs to be generated elsewhere
    table.insert( self.Perks, perk )
  end
  
  self.RemovePerk = function( perk )
    for n, v in pairs( self.Perks ) do
      if v == perk then
        table.remove( self.Perks, n )
      end
    end
  end
  
  self.GetName = function() return self.Name   end
  self.GetEnt  = function() return self.Entity end
  
  self.SetName = function( n )   self.Name   = n   end
  self.SetEnt  = function( ent ) self.Entity = ent end
  
  self.AddFunds = function( amount )
    self.Money = self.Money + amount
  end
  
  self.Purchase = function( item )
    if item.Cost <= self.Money then
      print( self.Name, "Purchased", item.Name )
      self.Inventory.Add( item )
      self.Money = self.Money - item.Cost
    end
  end
  
  self.Sell = function( item )
    if self.Inventory.Contains( item ) then
      self.Money = self.Money + item.Cost
      self.Inventory.Remove( item )
    end
  end
  
  self.print   = function() 
    print( "Name:", self.Name )
    print( "Team:", self.Team.Name )
    print( "Entity:", self.Entity )
    print( "Cash:", self.Money ) 
    print( "Perks:" )
    
    for n, v in pairs( self.Perks ) do
      print( n, v.Name, v.Type, v.Tier, v.Description )
    end
    self.Inventory.print()
  end
  
  return self
  
end

-- Player Inventory Constructor
-- 
-- Inputs:
--  Must be attached to a given player
-- 
-- Returns:
--  newly constructed inventory item
-- 
Teams.Player.Inventory.new = function( player )
  
  local self = {}
  
  self.Items  = {}
  self.Player = player 
  self.Name   = player.Name
  self.Items.Count = 0
  
  self.Add = function( item )
    
    if self.Items.Count < Teams.Player.Inventory.Limit then
    
      self.Items.Count = self.Items.Count + 1
      table.insert( self.Items, item ) 
    
    end  
  end
  
  self.Remove = function( item )
    for n, v in pairs( self.Items ) do
      if v == item then
        table.remove( self.Items, n )
        self.Items.Count = self.Items.Count - 1
      end
    end
  end
  
  self.Contains = function( item )
    
    for n, v in pairs( self.Items ) do
      if v == item then
        return true
      end
    end
    
    return false
  end
  
  self.print =  function()
    print( self.Player.Name, "'s Inventory:" ) 
    for n, v in pairs( self.Items ) do
      
      if not (v == self.Items.Count) then
        print( n, v.Name )
      else
        print( n, v )
      end
    end
    
    print( "" )
    
  end
  
  return self
  
end

-- Inventory Item Constructor
-- 
-- Inputs:
--  name - the name of the item. MUST BE PASSED IN CONSTRUCTOR
-- 
-- Returns:
--  newly constructed item!
-- 
Teams.Player.Inventory.Item.new = function( name )
  
  local self = {}
  
  self.Name = name 
  self.Cost = 0
  
  self.print = function()
    print( self.Name )
  end
  
  return self
  
end

-- Perk Constructor
-- 
-- Inputs:
--  team - the team of the perk. MUST BE PASSED IN CONSTRUCTOR
-- 
-- Returns:
--  newly constructed perk!
-- 
Teams.Player.Perk.new = function( team )
  
  local self
  self = {}
  
  self.Team = team
  self.Name = nil
  self.Type = nil
  self.Tier = 0
  self.Action = nil
  self.Description = "It's a perk"
  
  return self
  
end
